package com.sihan.framework.etm.service.impl;


import com.framework.model.PageResult;
import com.framework.mybatis.Condition;
import com.framework.mybatis.service.impl.BaseServiceImpl;
import com.framework.util.PageUtils;
import com.github.pagehelper.PageHelper;
import com.sihan.framework.etm.common.CacheOperatorInfo;
import com.sihan.framework.etm.common.util.EtmWebUtil;
import com.sihan.framework.etm.dto.PageDevicePush;
import com.sihan.framework.etm.entity.DevicePush;
import com.sihan.framework.etm.mapper.DevicePushMapper;
import com.sihan.framework.etm.service.DevicePushService;
import com.sihan.framework.etm.vo.DevicePushVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DevicePushServiceImpl extends BaseServiceImpl<DevicePush,String> implements DevicePushService {

    @Autowired
    private DevicePushMapper devicePushMapper;
    @Override
    public PageResult<DevicePushVO> pageListDevicePushVO(PageDevicePush pageDevicePush) {
        PageHelper.startPage(pageDevicePush);
        String orgIds = (String) CacheOperatorInfo.getMapValue(EtmWebUtil.userId(), null);
        if (StringUtils.isBlank(orgIds)) {
            return PageUtils.toPageResult(new ArrayList<>());
        }
        List<DevicePushVO> devicePushVOS = devicePushMapper.pageListDevicePushVO(pageDevicePush,orgIds.split(","));
        PageResult<DevicePushVO> pageResult = PageUtils.toPageResult(devicePushVOS);
        return pageResult;
    }

    @Override
    public DevicePush selectByDeviceId(String deviceId) {

        return devicePushMapper.selectOne(new Condition<>().eq("device_id",deviceId).neq("state",0));
    }
}
